<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="blessId">
        <el-link type="primary" :underline="false">{{ blessInfo.name }}</el-link>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.resourceType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sourceType:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:sourceType:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sourceType:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sourceTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.resourceType" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="资源" align="left" prop="fileUrl">
        <template slot-scope="scope">
          <image-preview :src="scope.row.fileUrl" :width="100" :height="100" v-if="scope.row.type == 'img'"/>
          <audio ref="player"
                 :src="baseUrl + scope.row.fileUrl"
                 controls="controls"
                 preload="true"
                 @canplay="startPlay"
                 @ended="ended"
                 @timeupdate="timeupdate"
                 v-if="scope.row.type == 'music'"
          ></audio>

          <video
            v-if="scope.row.type == 'video'"
            width="734"
            height="434"
            id="videojs"
            class="video-js vjs-default-skin"
            crossorigin="anonymous"
            controls
            playsinline
          >
            <source
              :src="baseUrl + scope.row.fileUrl"
              id="videoVr-1"
              type="video/mp4"
            />
          </video>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sourceType:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sourceType:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type" v-if="form.id == null">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.resourceType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资源路径">
          <file-upload v-model="form.fileUrl" v-if="form.type == 'music'" :limit="1" :fileSize="20"
                       :fileType="['mp3']"/>
          <file-upload v-model="form.fileUrl" v-if="form.type == 'video'" :limit="1" :fileSize="20"
                       :fileType="['mp4']"/>
          <image-upload v-model="form.fileUrl" :limit="1" v-if="form.type == 'img'"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSourceType, getSourceType, delSourceType, addSourceType, updateSourceType} from "@/api/system/sourceType";
import {getBless} from "@/api/system/bless";


export default {
  name: "SourceType",
  dicts: ['resourceType'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源表格数据
      sourceTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        blessId: null,
        type: null,
        fileUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      blessId: null,
      blessInfo: {},
      baseUrl: process.env.VUE_APP_BASE_API
    };
  },
  created() {
    const id = this.$route.params && this.$route.params.id;
    this.blessId = id
    this.queryParams.blessId = this.blessId
    this.getBlessInfo()
    this.getList();
  },
  methods: {
    getBlessInfo() {
      getBless(this.blessId).then(response => {
        this.blessInfo = response.data;
      });
    },
    /** 查询资源列表 */
    getList() {
      this.loading = true;
      listSourceType(this.queryParams).then(response => {
        this.sourceTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        blessId: null,
        type: 'img',
        fileUrl: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.blessId = this.blessId
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.blessId = this.blessId
      this.open = true;
      this.title = "添加资源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSourceType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资源";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSourceType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSourceType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资源编号为"' + ids + '"的数据项？').then(function () {
        return delSourceType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sourceType/export', {
        ...this.queryParams
      }, `sourceType_${new Date().getTime()}.xlsx`)
    },
    //获取链接后准备播放
    startPlay() {
      let player = this.$refs.player;
      this.$store.commit('setDuration', player.duration);
      //开始播放
      player.play();
      this.$store.commit('setIsPlay', true);
    },//播放完成之后触发
    ended() {
      this.$store.commit('setIsPlay', false);
      this.$store.commit('setCurTime', 0);
      this.$store.commit('setAutoNext', true);
      this.next()
    },
    //开始、暂停
    togglePlay() {
      let player = this.$refs.player;
      if (this.isPlay) {
        player.play();
      } else {
        player.pause();
      }
    },
    //音乐播放时记录音乐的播放位置
    timeupdate() {
      this.$store.commit('setCurTime', this.$refs.player.currentTime);
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = { path: "/blessManage/bless" };
      this.$tab.closeOpenPage(obj);
    },
  }
};
</script>
